package com.sinoglobal.ningxia.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.utils.ToastBuilder;
import com.sinoglobal.wallet.util.KeyBoardUtils;

/**
 * Created by XINDZ on 2017/2/14.
 * 功能:评论的Dialog
 */

public class CommentDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private EditText commentContent;
    private TextView commentSend;
    private AddComment addComment;
    private InputMethodManager imm;
    private String comment;
    private String addUserName = "";


    public void setAddComment(AddComment addComment) {
        this.addComment = addComment;
    }

    public interface AddComment {
        void onAddComment(String str);
    }

    public CommentDialog(Context context) {
        super(context, R.style.CommentDialog);
        this.context = context;
    }


    @Override
    public void show() {
        super.show();
        if (addUserName.isEmpty()) {
            addUserName = "我来说两句";
        }
        commentContent.setHint(addUserName);
        KeyBoardUtils.openKeybord(commentContent, context);
    }

    @Override
    public void dismiss() {
        // TODO Auto-generated method stub
        KeyBoardUtils.closeKeybord(commentContent, context);
        super.dismiss();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.window_comment);
        commentContent = (EditText) findViewById(R.id.et_comment_content);
        commentSend = (TextView) findViewById(R.id.tv_send);
        setCanceledOnTouchOutside(true);
        initButton();
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = 0;
        params.y = 0;
        params.width = context.getResources().getDisplayMetrics().widthPixels;
        params.gravity = Gravity.BOTTOM;

        commentSend.setOnClickListener(this);
        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        commentContent.setFocusable(true);
        commentContent.setFocusableInTouchMode(true);
        commentContent.requestFocus();

    }

    private void initButton() {
        commentContent.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable editable) {
                comment = editable.toString().trim()/* +addUserName */;
                if (editable.length() > 0) {// 改变字体颜色
                    if (editable.length() == 200) {
                        ToastBuilder.getIntance(context).showToast("评论最多可输入200个字");
                    }
                    commentSend.setTextColor(context.getResources().getColor(R.color.send));
                } else {
                    commentSend.setTextColor(context.getResources().getColor(R.color.send_default));
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_send:

                if (commentContent.getText().toString().trim().isEmpty()
                        && commentContent.getText().toString().trim().equals("")) {
                    Toast.makeText(context, "请输入内容!", Toast.LENGTH_SHORT).show();
                    return;
                } else if (addComment != null) {
                    addComment.onAddComment(comment);
                    comment = "";
                    commentContent.setHint("");
                    commentContent.setText("");

                    KeyBoardUtils.closeKeybord(commentContent, context);

                }

                dismiss();
                break;

            default:
                break;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.KEYCODE_BACK == keyCode) {
            dismiss();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public String getTs() {
        // return mTs;
        return comment;
    }

    public String getStringContent(String addEditContent) {

        addUserName = addEditContent;

        return addUserName;
    }

}
